# Author ： chosen-yn
from rest_framework import serializers
from .models import CourseCategory, Course, Teacher, CourseChapter, CourseSection


class CourseCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = ['id', 'name']
        extra_kwargs = {}


class TeacherSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = [
            'id',
            'name',
            'role',
            'title',
            'signature',
            'image',
            'brief',
        ]


class CourseSerializer(serializers.ModelSerializer):
    teacher = TeacherSerializer()

    class Meta:
        model = Course
        fields = [
            'id',
            'name',
            'price',
            'course_img',
            'students',
            'sections',  # 总课时
            'pub_sections',  # 发布课时更新数量

            # 列表页面不显示，详情接口会显示
            'period',  # 建议学习周期
            'brief',
            'attachment_path',  # 文档地址

            # choice字段，定制返回格式 --》 表模型中写
            'course_type_name',
            'level_name',
            'status_name',

            # 关联表--
            'teacher',  # teacher 所有数据---》子序列化写法
            'section_list',  # 表模型：返回课时：如果总课时数，大于4，就返回4条，如果小于4，有多少返回多少--表模型
        ]


class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseSection
        fields = [
            'id',
            'name',
            'orders',
            'section_link',
            'duration',
            'free_trail'
        ]


class CourseChapterSerializer(serializers.ModelSerializer):
    coursesections = CourseSectionSerializer(many=True)

    class Meta:
        model = CourseChapter
        fields = ['id', 'name', 'coursesections', 'chapter', 'summary']


class CourseDetailSerializer(serializers.ModelSerializer):
    teacher = TeacherSerializer()
    coursechapters = CourseChapterSerializer(many=True)

    class Meta:
        model = Course
        fields = [
            'id',
            'name',
            'price',
            'course_img',
            'students',
            'sections',  # 总课时
            'pub_sections',  # 发布课时更新数量

            # 列表页面不显示，详情接口会显示
            'period',  # 建议学习周期
            'brief',
            'attachment_path',  # 文档地址

            # choice字段，定制返回格式 --》 表模型中写
            'course_type_name',
            'level_name',
            'status_name',

            # 关联表--
            'teacher',  # teacher 所有数据---》子序列化写法
            'coursechapters'
        ]
